﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Studio77.GuessLyric.Core;
using System.Windows.Threading;

namespace Studio77.GuessLyric
{
    public partial class MainPage : PhoneApplicationPage
    {
        private static bool isShowOpenAd = false;

        // 构造函数
        public MainPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            try
            {
                for (int i = 0; i < 3; i++)
                {
                    this.NavigationService.RemoveBackEntry();
                }
            }
            catch (Exception)
            {
            }

            if (StageManager.Instance.LastStage >= StageManager.Instance.Questions.Count)
            {
                this.txtCurrentStage.Text = "完成";
            }

            this.btnNewUser.Visibility = System.Windows.Visibility.Collapsed;
            if (StageManager.Instance.IsDoNewUserTask)
            {
                this.btnNewUser.Visibility = System.Windows.Visibility.Collapsed;

            }
            else if (UmengSDK.UmengAnalytics.GetOnlineParam("useGift") == "1")
            {
                this.btnNewUser.Visibility = System.Windows.Visibility.Visible;
            }

            this.StoryboardShow.Begin();
            this.txtCurrentStage.Text = StageManager.Instance.LastStage.ToString();

            String usemokaStr = UmengSDK.UmengAnalytics.GetOnlineParam("usemoka");
            //0-关闭 1-同时开启 2-开启banner 3-开启开屏
            if ((usemokaStr == "1" || usemokaStr == "3") && !isShowOpenAd)
            {
                isShowOpenAd = true;
                DispatcherTimer timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromSeconds(1);
                timer.Tick += (object ss, EventArgs ee) =>
                {
                    timer.Stop();
                    string dateStr = DateTime.Now.ToString("yyyyMMdd") + (DateTime.Now.Hour < 12 ? "A" : "P");
                    if (StageManager.Instance.LastFullScreenDate != dateStr)
                    {
                        StageManager.Instance.LastFullScreenDate = dateStr;
                        NavigationService.Navigate(new Uri("/Game77SharedLib;component/MokaPage.xaml?channel=fkcgc", UriKind.RelativeOrAbsolute));
                    }
                };
                timer.Start();

            }
        }

        private void btnStart_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.enter);
            if (StageManager.Instance.LastStage <= StageManager.Instance.Questions.Count)
            {
                //进入游戏
                this.NavigationService.Navigate(new Uri("/Pages/GamePage.xaml", UriKind.RelativeOrAbsolute));
            }
            else
            {
                MessageBox.Show("亲，您已经完成所有关卡了！可以选择重新游戏或者等待更新哦！");
            }
        }

        private void btnNewUser_Click(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.enter);
            this.NavigationService.Navigate(new Uri("/Pages/NewUserPage.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}